package com.example.administrator.student4;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

import org.json.JSONException;

import java.io.IOException;

/**
 * Created by Administrator on 2016/11/30.
 */
public class SearchByClassAndSubject extends Fragment {
    Spinner sp_subject, sp_class, sp_term;
    Button search;
    String  subject,class_a;
    String[] arr_class,arr_subject;
    View view;
    Myhandler myhandler;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.searchcalssandsubject, container, false);
        uiinit();
        myhandler = new Myhandler();
        new Thread(){
            @Override
            public void run() {
                super.run();
                try {
                    arr_subject = WebUtil.subjectfind();
                    arr_class = WebUtil.classfind();
                    myhandler.sendEmptyMessage(0x111);
                } catch (IOException | JSONException e) {
                    e.printStackTrace();
                }
            }
        }.start();
        search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                class_a = sp_class.getSelectedItem().toString();
                subject = sp_subject.getSelectedItem().toString();
                Intent intent = new Intent(getActivity(),SearchByClass.class);
                intent.putExtra("class_a",class_a);
                intent.putExtra("subject",subject);
                startActivity(intent);
            }
        });
        return view;
    }
    public void uiinit(){
        sp_subject = (Spinner) view.findViewById(R.id.subject);
        sp_class = (Spinner) view.findViewById(R.id.class_a);
        search = (Button) view.findViewById(R.id.search);

    }
    class Myhandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what==0x111) {
                ArrayAdapter<String> adapter_class = new ArrayAdapter<>(getActivity(), android.R.layout.simple_expandable_list_item_1, arr_class);
                ArrayAdapter<String> adapter_subject = new ArrayAdapter<>(getActivity(), android.R.layout.simple_expandable_list_item_1, arr_subject);
                sp_subject.setAdapter(adapter_subject);
                sp_class.setAdapter(adapter_class);
            }
        }
    }
}
